<?php
if(@$_POST['open'] != 'please'){
	exit;
}
include_once("../../php/include_all.php");
?>
<div class="row-fluid">
	<div class="span12">
		<h3 class="page-title">
            <b>Master Data Tahun Anggaran</b>
        </h3>	
			        <a href="#modalwin" data-toggle="modal" class="btn btn-primary btn-tambah">Tambah Data</a>
        <div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

            <header class="modal-header">
			<a href="#" class="close geo-clear" data-dismiss="modal">x</a>
                <h3>Data Tahun Anggaran</h3>
            </header>

            <div class="modal-body">
                <form id="form_tahun" class="form-horizontal" >
                    <input type="hidden" name="act" value="tahun_action">
                    <input type="hidden" id="id_thn_angg" name="id_thn_angg" value="0">
                    <table class="table-form">
                        <tbody>
                        <div class="control-group">
                            <label class="control-label" for="des">Tahun Anggaran :</label>
                            <div class="controls">  
                                <input type="text" id="thn_angg" placeholder="tahun anggaran" name="thn_angg" >
                                <a class="btn btn-primary bt-simpan-tahun"><div id="loader" style="margin-top:2px"></div>&nbsp;Simpan</a>
                                <span id="result"></span>
                            </div>
                        </div>
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
		</div>
	</div>
	</br>
	<div class="row-fluid">
        <div class="span12">
			<div class="widget" style="">
                <div class="widget-title">
                    <h4><i class="icon-reorder"></i>Tabel Tahun Anggaran</h4>
                    <span class="tools">
                        <a href="javascript:;" class="icon-chevron-down"></a>
                    </span>
                </div>
				<div class="widget-body">
                    <table class="table table-striped table-bordered" id="sample_1">
                    <thead>
                        <tr>
                            <th class="hidden-phone" style="width:20px; text-align:center;">No</th>
                            <th style="text-align:center; ">Tahun Anggaran</th>
							<th style="text-align:center;">Edit</th>
                        </tr>
                    </thead>
                    <tbody id="tbody_tahun">
					<?php 
					$no=1;
					$thn_angg=get_datas("SELECT * FROM spm_thn_angg");
					foreach($thn_angg as $thn_angg){
					?>
						<tr>
							<td><?php echo $no ?></td>
							<td style="text-align:center;"><?php echo $thn_angg['thn_angg']?></td>	
							<td style="text-align:center; width:200px;">
							<a class="btn btn-danger bt-delete-thn_angg" name="<?php echo $thn_angg['id_thn_angg']; ?>">Delete</a>
                                <a  href="#modalwin" data-toggle="modal"  class="btn btn-info bt-edit-thn_angg" name="<?php echo $thn_angg['id_thn_angg']; ?>">Edit</a>
							</td>
							
						</tr>
						<?php 
						$no++;
					}
					?>
					</tbody>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript" src="../assets/data-tables/jquery.dataTables.js"></script>
<script type="text/javascript" src="../assets/data-tables/DT_bootstrap.js"></script>
<script>
    jQuery(document).ready(function() {       
         // initiate layout and plugins
        App.init();
    });
</script>

<script>
var urls = 'master_tahun/tahun_action.php';

$('.bt-simpan-tahun').click(function(){
	var btn  = $(this);
	var load = $('#loader');
	var rslt = $('#result');
	load.addClass('spinner pull-left');
	btn.removeClass('btn-primary').addClass('btn-info');
	
	var form = $('#form_tahun');
	var data = form.serializeArray();
	var post = $.post(urls,data);
	post.done(function(res){
		var result = res.split('__');
		//   0      1     2
		//success__id_th__(isi tbody)tr
		if(result.length==3){
			if(result[0]=='success'){
				rslt.html('<font color="green">tersimpan...</font>');
				setTimeout(function(){
					rslt.html('');
				},1500);
				var tbody = $('#tbody_tahun');
				tbody.html(result[2]);
				document.getElementById('form_tahun').reset();
				 //$('#thn_angg').val(' ');
				init();
			}else{
				rslt.html('<font color="red">'+res+'</font>');
			}
		}else{
			rslt.html('<font color="red">'+res+'</font>');
		}
		load.removeClass();
		btn.removeClass('btn-info').addClass('btn-primary');
	});
});
	
function init(){
	$('.bt-delete-thn_angg').click(function(){
		var btn  = $(this);
		var pid  = this.name;
		var load = $(this);
		load.html('<div class="spinner"></div>');
		bootbox.confirm("Anda akan menghapus data ini ?", function(result) {
			if(result==true){
				if(pid!=0){
					var post = $.post(urls,{act:"del_thn_angg",id_thn_angg:pid});
					post.done(function(res){
					
					var hasil = res.split('__');
						if(hasil.length==2){
							if(hasil[0] == 'success'){
								var tbody = $('#tbody_tahun');
								tbody.html(hasil[1]);
								init();
									
							}
						} 
					});
					load.html('Delete');
				}
			}else {
			load.html('Delete');
			}
		});
	});
	
	$('.bt-edit-thn_angg').click(function(){
		var id_thn_angg = this.name;
		var form = $('#form_tahun');
		var post = $.post(urls,{act:'edit_thn_angg',id_thn_angg:id_thn_angg});
		post.done(function(res){
			var value = res.split('__');
			$('#id_thn_angg').val(value[0]);
			form.find('input[name=thn_angg]').val(value[1]);
		});
	});
}

$('.geo-clear').click(function(){
	$('#id_thn_angg').val(0);
	document.getElementById('form_tahun').reset();
});
init();
</script>